load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
load("@rules_cc//cc:cc_test.bzl", "cc_test")

# Common defines for all test binaries
COMMON_DEFINES = [
    "HAVE_FNMATCH_H",
    "HAVE_GETEUID",
    "HAVE_GETUID",
    "HAVE_GLOB_H",
    "HAVE_ICONV",
    "HAVE_LANGINFO_H",
    "HAVE_MBSRTOWCS",
    "HAVE_SETREUID",
    "HAVE_SETUID",
    "HAVE_SRANDOM",
    "HAVE_STDALIGN_H",
    "HAVE_STPCPY",
    "HAVE_STRERROR",
    "HAVE_VASPRINTF",
]

cc_binary(
    name = "tdict",
    srcs = [
        "tdict.c",
    ],
    copts = [
        "-Wall",
    ],
    defines = COMMON_DEFINES,
    deps = [
        "//:popt",
    ],
)

cc_binary(
    name = "test2",
    srcs = [
        "test2.c",
    ],
    copts = [
        "-Wall",
    ],
    defines = COMMON_DEFINES,
    visibility = ["//visibility:public"],
    deps = [
        "//:popt",
    ],
)

# Test1 test cases
# Note: Tests requiring aliases from test-poptrc config file are excluded
TEST1_CASES = {
    "test1_01_arg1": ["--arg1"],
    "test1_02_arg2": [
        "--arg2",
        "foo",
    ],
    "test1_03_both_args": [
        "--arg1",
        "--arg2",
        "something",
    ],
    "test1_09_short_opt": [
        "-2",
        "foo",
    ],
    "test1_10_arg3": [
        "-3",
        "50",
    ],
    "test1_14_inc": ["--inc"],
    "test1_15_inc_with_arg2": [
        "-I",
        "--arg2",
        "foo",
    ],
    "test1_18_callback": [
        "--arg1",
        "--cb",
        "bar",
    ],
    "test1_26_callback2": [
        "--cb2",
        "foo",
    ],
    "test1_32_int": [
        "-i",
        "123456789",
    ],
    "test1_33_int_long": [
        "--int",
        "123456789",
    ],
    "test1_34_short": [
        "-s",
        "12345",
    ],
    "test1_35_short_long": [
        "--short",
        "12345",
    ],
    "test1_36_long": [
        "-l",
        "1123456789",
    ],
    "test1_37_long_long": [
        "--long",
        "1123456789",
    ],
    "test1_38_longlong": [
        "-L",
        "1111123456789",
    ],
    "test1_39_longlong_long": [
        "--longlong",
        "1111123456789",
    ],
    "test1_40_float": [
        "-f",
        "10.1",
    ],
    "test1_41_float_long": [
        "--float",
        "10.1",
    ],
    "test1_42_double": [
        "-d",
        "10.1",
    ],
    "test1_43_double_long": [
        "--double",
        "10.1",
    ],
    "test1_44_optional_no_arg": ["--optional"],
    "test1_45_optional_equals": ["--optional=yadda"],
    "test1_46_optional_space": [
        "--optional",
        "yadda",
    ],
    "test1_49_argv": [
        "--argv",
        "A",
        "--argv",
        "B",
        "C",
    ],
    "test1_52_bitxor": ["--bitxor"],
    "test1_53_bitset": ["--bitset"],
    "test1_54_bitclr": ["--bitclr"],
    "test1_55_nobitset": ["--nobitset"],
    "test1_56_nobitclr": ["--nobitclr"],
    "test1_57_bits": [
        "--bits",
        "foo,bar,baz,!bar",
    ],
}

[cc_test(
    name = name,
    srcs = ["test1.c"],
    args = args,
    copts = ["-Wall"],
    defines = COMMON_DEFINES,
    deps = ["//:popt"],
) for name, args in TEST1_CASES.items()]

# Test3 test cases with data files
TEST3_CASES = {
    "test3_01": "01",
    "test3_02": "02",
    "test3_03": "03",
}

[cc_test(
    name = name,
    srcs = ["test3.c"],
    args = ["$(rootpath test3-data/{}.input)".format(num)],
    copts = ["-Wall"],
    data = [
        "test3-data/{}.input".format(num),
        "test3-data/{}.answer".format(num),
    ],
    defines = COMMON_DEFINES,
    target_compatible_with = select({
        # Windows will require runfiles and thus `rootpath` in `args`
        # will not result in a file being found.
        "@platforms//os:windows": ["@platforms//:incompatible"],
        "//conditions:default": [],
    }),
    deps = ["//:popt"],
) for name, num in TEST3_CASES.items()]
